package com.zhou.message.dao;

import java.math.BigDecimal;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.zhou.message.common.AttachSourceEnum;
import com.zhou.message.model.MessageReceiver;
import com.zhou.org.dao.Org;
import com.zhou.org.dao.User;
import lombok.Data;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 
 * </p>
 *
 * @author lang.zhou
 * @since 2023-03-01
 */
@Data
@TableName("sys_send_message")
public class SendMessage implements Serializable {

    private static final long serialVersionUID=1L;

    /**
     * 发送消息表
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private Integer fromUserId;

    private String title;

    private String content;

    /**
     * 1-待发送;2-已发送
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createDt;

    /**
     * 是否定时发送;0-否;1-是
     */
    private Integer timer;

    /**
     * 定时发送时间
     */
    private Date sendDt;

    /**
     * 消息优先级0-10
     */
    private Integer level;

    /**
     * 0-内部消息；1-邮件；3-三方消息
     */
    private Integer type;

    private Integer deleteFlag;

    @TableField(exist = false)
    private List<MessageReceiver> receiver = new ArrayList<>();
    @TableField(exist = false)
    private List<Attachment> attachmentList = new ArrayList<>();

    public void addReceiver(Object o){
        if(o instanceof User){
            receiver.add(new MessageReceiver((User) o));
        }else if(o instanceof Org){
            receiver.add(new MessageReceiver((Org) o));
        }else if(o instanceof MessageReceiver){
            receiver.add((MessageReceiver) o);
        }else if(o instanceof MessageTemplateReceiver){
            MessageTemplateReceiver t = (MessageTemplateReceiver) o;
            MessageReceiver m = new MessageReceiver();
            m.setOrgId(t.getOrgId());
            m.setOrgType(t.getOrgType());
            m.setUserId(t.getUserId());
            receiver.add(m);
        }else if(o instanceof Collection){
            receiver.addAll((Collection<? extends MessageReceiver>) o);
        }else if(o.getClass().isArray()){
            receiver.addAll(Arrays.asList((MessageReceiver[]) o));
        }
    }
    public void addAttachment(Object o){
        if(o instanceof Attachment){
            Attachment a = (Attachment) o;
            a.setSource(AttachSourceEnum.MSG.getEnumValue());
            attachmentList.add(a);
        }else if(o instanceof Collection){
            Collection<Attachment> as = (Collection<Attachment>) o;
            for (Attachment a : as) {
                a.setSource(AttachSourceEnum.MSG.getEnumValue());
            }
            attachmentList.addAll(as);
        }else if(o.getClass().isArray()){
            Attachment[] as = (Attachment[]) o;
            for (Attachment a : as) {
                a.setSource(AttachSourceEnum.MSG.getEnumValue());
            }
            attachmentList.addAll(Arrays.asList(as));
        }
    }

}
